#include "Bomb.h"
#include "Physics.h"

Bomb::Bomb(const Vector2df p, const Vector2df v) : StatefulUnit(p,v)
{
    loadSprite("Bomb.sprite");
    setSpriteState("Bomb");
    m_blownUp = false;
};

Bomb::~Bomb()
{
}

int Bomb::getState() const {
    return usPICKUPABLE;
}

void Bomb::hit(const SimpleDirection dir, BaseUnit* player, CRstring parameter) {
    if (getSpriteState()=="Bomb") {
        player->hit(dir.opposite(),this);
    }
}

void Bomb::customUpdate()
{
    // Time to blow up!
    // Note that the BlowingUp animation is 201 pixels wide, 97 pixels high, the normal image is 28 pixels wide, 51 pixels high
    // So to center the explosion, we need to adjust our position to the left by (201/2)-(28/2) = 86 pixels
    // and up (97-51) = 46 pixels
    // I may be wrong on this, but it is a starting guess.
    m_counter.stop();
    incPosition(Vector2df(-86.00,-46.00));
    setSpriteState("BlowingUp");
    m_blownUp = true;
};

void Bomb::update()
{
    StatefulUnit::update(); // Do the normal stuff
    if (m_blownUp && hasFinished()) {
        m_shouldBeRemoved = true;
    }
};

void Bomb::hitMap(MapData* const data, const CollisionObject* const obj)
{
    Vector2df correction = obj->getTotalCorrection();

    if (correction != Vector2df(0,0))
    {
        handleEvent("HitMap");
        if (isBeingCarried())
        {
            resetParent();
            releaseFromBeingCarried(obj->getCollisionDirection(getPosition()));
            return;
        }
        if (m_state == usPROJECTILE)
        {
            correction = data->breakTiles(obj,1,true,true);
        }

    }

    incPosition(PHYSICS->hardCollision(m_velocity,correction));
}

void Bomb::resetParent()
{
    BaseUnit::resetParent();
    setSpriteState("FuseLit"); // Need to figure out how to detect the end of an animation and change states, to allow a single jump
    m_counter.start(5000);
}
